package CSound
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	public class ps_volume extends Object {
		private var _player: ps_player;
		private var _soundBox: Object;
		public function ps_volume(a_player: ps_player) {
			super();
			_player = a_player;
			
			var styles: Object = pj.styles;
        	styles['soundTrack'] = {
        		line_color	: 0,
        		line_width	: 1,
        		line_alpha	: 0.5,
        		padding		: 1,
        		fill_color	: 0x0000FF,
        		fill_alpha	: 0.3 
        	}
        	
        	styles['track'] = {
        		fill_color	: 0,
        		fill_alpha	: 1 
        	}
        	
        	styles['sb_sb'] = pj.classes.util.union(styles['scrollbar'], {
				line_width: 0,
				rounds: 10
        	});
        	
        	styles['sound_box'] = pj.classes.util.union(styles['scrollbar'], {
				line_width: 0,
				rounds: 10,
				filters: {
					glow: {
						color: 0xFFFFFF
					}
				}
        	});
        				
			createBox(_player.card, cardSource.x + cardSource.width / 2 - 30, cardSource.y + cardSource.height - 35);
		}
		
		public function get cardSource(): Object {
			return _player.card.card_mask.source;
		}
		
		public function dispose(): void {
			
		}
		
		private function get pj(): Object {
			return _player.card.app;
		}
		
        private function createBox(parent: Object, x: Number, y: Number): void {
        	var indCount: int = 6;
        	var prevValue: Number = 0;
        	_soundBox = new pj.classes.component(parent, x, y, 60, 30, 'sound_box');
        	var sb: Object = new pj.classes.scrollbar(_soundBox, 0, 0, 60, 30, 'sb_sb');
        	sb.value = Math.round(100 * _player.volume);
      		prevValue = sb.value;
	      	
	      	sb.addEvent(pj.classes.scrollbar.SCROLLING, function(e: Event): void {
	      		_player.volume 	= sb.value / 100;
	      		prevValue 		= sb.value;
	      	});
	      	
        	var box: Object = new pj.classes.buttonPJ(_soundBox, 0, 0, 60, '', function(e: Event): void {
        		if (sb.value > 0) {
        			sb.fx('value', sb.value, 0).start();
        			_player.volume = 0;
        		} else {
        			sb.fx('value', sb.value, prevValue).start();
        			_player.volume = prevValue / 100;
        		}
        	});
        	
        	_soundBox.addEvent(MouseEvent.ROLL_OVER, function(e: Event): void {
        		sb.fx('x', sb.x, 60).start();
        		_soundBox.fx('width', _soundBox.width, 120).start();
        	});
        	_soundBox.addEvent(MouseEvent.ROLL_OUT, function(e: Event): void {
        		sb.fx('x', sb.x, 0).start();
        		_soundBox.fx('width', _soundBox.width, 60).start();
        	});
        	var inner: Rectangle = box.getInner();
        	var layer: Object = new pj.classes.component(box, inner.x + inner.width, inner.y + inner.height, inner.width, inner.height, 'soundTrack');
        	layer.rotation = 180;
        	box.swapChildren(layer, box.hotspot);

        	inner = layer.getInner();
        	var indSize: Number 	= inner.width / indCount;
        	
        	for (var i: int = 0; i<indCount; i++) {
        		var line: Object = new pj.classes.component(layer, inner.x + i * indSize + 1, inner.y, indSize - 1, inner.height, 'track');
        	}
        	
        	var s2: Number = 0;
        	var s3: Number = 0;
        	var fac: Number = 0.01;
        	
        	layer.addEvent(Event.ENTER_FRAME, function(e: Event): void {
        		if (Math.random() > 0.95)
        			fac = 0.01 + Math.random() * 2;
        		for (var i: int =0; i<layer.numChildren; i++) {
        			s2 -= Math.random() * 0.2;
        			s3 += Math.random() * 0.11;
					var line: Object = layer.getChildAt(i);
					line.height = 2 + (Math.sin(s2) + Math.sin(s3 + i * fac) + 2) / 4 * (inner.height - 2) * sb.value / 100;
        		}
        	});
        };		
	}
}